<?php
/**
 * Created by PhpStorm.
 * User: xaq
 * Date: 2017/5/12
 * Time: 10:07
 */

namespace app\api\model;

use think\Model;
use think\Db;

class Banner extends BaseModel
{
    protected $hidden = ['update_time','delete_time'];
    public function items() {
        //一对多的关系
        return $this->hasMany('BannerItem','banner_id','id');
    }

    public static function getBannerById($id){
           // 1、基本使用
//        $result = Db::query(
//            'select * from banner_item where banner_id=?',[$id]);
           // 2、查询构造器find()单条记录，select()多条记录
//        $banner = Db::table('banner_item')->where('banner_id','=',$id)->select();

         //2.1 查询构造器闭包写法
//        $result = Db::table('banner_item')
//            ->where(function ($query) use ($id){
//                $query->where('banner_id','=',$id);
//            })->select();
//         return $result;
        $banner = self::with(['items','items.img'])->find($id);
        return $banner;
    }


}